package com.app.controller;

import com.app.common.Result;
import com.app.model.DepartmentModel;
import com.app.model.PlanBusinessModel;
import com.app.model.PlanFundsModel;
import com.app.model.SystemModel;
import com.app.service.AttractInvestmentService;
import com.app.service.ProjectProgressService;
import com.app.service.SearchService;
import com.app.service.StriveForFundsService;
import com.app.vo.BusinessPlanVo;
import com.app.vo.FundsPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@RestController
@RequestMapping("/api/search")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @Autowired
    private StriveForFundsService striveForFundsService;
    @Autowired
    private AttractInvestmentService attractInvestmentService;
    @Autowired
    private ProjectProgressService projectProgressService;

    @GetMapping("/searchAll")
    public Result<Object> searchAll(String keyword) {
        if (keyword == null || keyword.equals("")) {
            return Result.success(null);
        }
        return Result.success(searchService.searchAll(keyword));
    }

    //曝光台查详情
    @GetMapping("/selectExposureById")
    public Result<Object> selectExposureById(Integer id) {
        return Result.success(searchService.selectExposureById(id));
    }

    //书记督查详情
    @GetMapping("/selectMatterById")
    public Result<Object> selectMatterById(Integer id) {
        return Result.success(searchService.selectMatterById(id));
    }

    //重点项目查详情
    @GetMapping("/selectProjectById")
    public Result<Object> selectProjectById(Integer id) {
        return Result.success(searchService.selectProjectById(id));
    }

    //重点工作查详情
    @GetMapping("/selectKeyWorkById")
    public Result<Object> selectKeyWorkById(Integer id,Integer departmentId) {
        if (departmentId != null && departmentId != 0) {
            return Result.success(searchService.selectKeyWorkByDepartmentId(departmentId));
        }
        return Result.success(searchService.selectKeyWorkById(id));
    }

    //争取自资金查详情
    @GetMapping("/selectFundsById")
    public Result<Object> selectFundsById(Integer departmentId) {
        PlanFundsModel model = new PlanFundsModel();
        model.setDepartmentId(departmentId);
        FundsPlanVo fundsPlanVo = striveForFundsService.searchPlan(model);
        List<DepartmentModel> collect = fundsPlanVo.getList().stream()
                .filter(PlanFundsModel -> Objects.equals(PlanFundsModel.getDepartmentId(), departmentId))
                .collect(Collectors.toList());
        List<SystemModel> systemModelList = projectProgressService.getSystemAll();
        for (int i = 0; i < collect.size(); i++) {
            for (int j = 0; j < systemModelList.size(); j++) {
                if(collect.get(i).getType()==systemModelList.get(j).getId())
                {
                    collect.get(i).setTypeText(systemModelList.get(j).getName());
                }
            }
        }
        return Result.success(collect);
    }

    //招商引资查详情
    @GetMapping("/selectBusinessById")
    public Result<Object> selectBusinessById(Integer departmentId) {
        PlanBusinessModel model = new PlanBusinessModel();
        model.setDepartmentId(departmentId);
        BusinessPlanVo businessPlanVo = attractInvestmentService.selectPlan(model);
        List<DepartmentModel> collect = businessPlanVo.getList().stream()
                .filter(PlanBusinessModel -> PlanBusinessModel.getDepartmentId() == departmentId)
                .collect(Collectors.toList());
        List<SystemModel> systemModelList = projectProgressService.getSystemAll();
        for (int i = 0; i < collect.size(); i++) {
            for (int j = 0; j < systemModelList.size(); j++) {
                if(collect.get(i).getType()==systemModelList.get(j).getId())
                {
                    collect.get(i).setTypeText(systemModelList.get(j).getName());
                }
            }
        }
        return Result.success(collect);
    }
}
